<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '150px'">
          <el-form-item label="名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
          <el-form-item label="广告主地址" prop="dspReportUrl">
          <el-input v-model="dataForm.dspReportUrl" placeholder="广告主上报的URL"></el-input>
      </el-form-item>
          <el-form-item label="广告主回调URL" prop="dspCallbackUrl">
          <el-input v-model="dataForm.dspCallbackUrl" placeholder="广告主回调URL"></el-input>
      </el-form-item>
          <el-form-item label="广告主密钥" prop="dspSalt">
          <el-input v-model="dataForm.dspSalt" placeholder="APIKey"></el-input>
      </el-form-item>
          <el-form-item label="媒体的上报URL" prop="sspApiUrl">
          <el-input v-model="dataForm.sspApiUrl" placeholder="媒体的上报URL"></el-input>
      </el-form-item>
          <el-form-item label="媒体的密钥" prop="sspKey">
          <el-input v-model="dataForm.sspKey" placeholder="媒体的key"></el-input>
      </el-form-item>
          <el-form-item label="广告主的扩展字段" prop="dspExtend">
          <el-input v-model="dataForm.dspExtend" placeholder="广告主的扩展字段"></el-input>
      </el-form-item>
          <el-form-item label="媒体的扩展字段" prop="sspExtend">
          <el-input v-model="dataForm.sspExtend" placeholder="媒体的扩展字段"></el-input>
      </el-form-item>
        
    
    <el-form :inline="true">
          <el-form-item label-width="150px" label="所属运营" prop="userName">
           <el-select v-model="dataForm.userName" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in userNameList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
          <el-form-item label-width="150px" label="所属商务" prop="business">
          <el-select v-model="dataForm.business" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in businessList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
      <el-form-item label-width="150px" label="所属产品"  prop="productChannel">
           <el-select v-model="dataForm.productChannel" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in productList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
      <el-form-item label-width="150px"   label="所属媒体" prop="mediaChannel">
         <el-select v-model="dataForm.mediaChannel" placeholder="选择运营人员" filterable clearable>
              <el-option v-for="item in mediaList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
            <el-form-item label-width="150px"   label="计费指标" prop="mediaChannel">
         <el-select v-model="dataForm.keyType" placeholder="计费指标" filterable clearable>
              <el-option v-for="item in typeList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
      <el-tooltip class="item" effect="dark" content="设置比例，例如通过率10%，设置0.1" placement="top-start">
          <el-form-item label-width="150px" label="回传比例" prop="per">
              <el-input v-model="dataForm.per" placeholder="回传比例"></el-input>
          </el-form-item>
        </el-tooltip>

         <el-tooltip class="item" effect="dark" content="不做限制，请不要填写！" placement="top-start">     
          <el-form-item label-width="150px"  label="当日限量" prop="dayLimitNumber">
                 <el-input v-model="dataForm.dayLimitNumber" placeholder="当日限量"></el-input>
          </el-form-item>
      </el-tooltip>

        <el-tooltip class="item" effect="dark" content="不做限制，请不要填写！" placement="top-start">     
          <el-form-item label-width="165px"  label="总数限量" prop="totalLimitNumber">
                <el-input v-model="dataForm.totalLimitNumber" placeholder="总数限量"></el-input>
          </el-form-item>
      </el-tooltip>
      <el-form-item label-width="150px"   label="账户状态" prop="mediaChannel">
         <el-select v-model="dataForm.status" placeholder="账户状态" filterable clearable>
              <el-option v-for="item in statusList " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>

      <el-form-item label-width="150px"   label="是否多监测" prop="mediaChannel">
          <el-radio-group v-model="dataForm.muiltReportInd">
            <el-radio :label="1">否</el-radio>
            <el-radio :label="2">是</el-radio>
          </el-radio-group>
      </el-form-item>
    </el-form>
    </el-form>
  

    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { getDictDataList } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        name: '',
        dspReportUrl: '',
        dspCallbackUrl: '',
        dspSalt: '',
        sspApiUrl: '',
        sspKey: '',
        dspExtend: '',
        sspExtend: '',
        createTime: '',
        updateTime: '',
        dspReportImpUrl: '',
        dspReportClickUrl: '',
        userName: '',
        business: '',
        templateId: '',
        dayLimitNumber:'',
        totalLimitNumber:'',
        muiltReportInd:1
      },
      templateList:[],
      userNameList:[],
      businessList:[],
      mediaList:[],
      productList:[],
      typeList:[],
      statusList:[]
    }
  },
  computed: {
    
  },
  mounted() {
    this.getTemplateList();
    this.getBusinessDict();
    this.getNameDict();
    this.getProductList();
    this.getMediaList();
    this.getTypeList();
    this.getStaus();
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },

    getTemplateList(){
      this.$http.get(`/adx/account/templateList`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.templateList=res.data;
      }).catch(() => {})
    },
   getNameDict() {
      this.userNameList = getDictDataList("userNameList")

    },
    getBusinessDict() {
      this.businessList = getDictDataList("businessList")
    },
       getMediaList() {
      this.mediaList = getDictDataList("mediaList")
    },
       getProductList() {
      this.productList = getDictDataList("productList")
    },
     getTypeList() {
      this.typeList = getDictDataList("typeList")
    },
     getStaus() {
      this.statusList = getDictDataList("account_status")
    },


    // 获取信息
    getInfo () {
      this.$http.get(`/adx/account/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/account/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
